<?php
namespace app\admin\controller;

use app\admin\model\Users;
use app\common\controller\AdminBase;
use think\Db;
/**
* 职位管理
*/
class Tj extends AdminBase
{
    
    protected function _initialize()
    {
        parent::_initialize();
    }
    public function index(){
        $list =  Db::query("SELECT b.count,a.* FROM `hs_users` a inner join (SELECT `otuijian`,count(*) as count FROM `hs_users` group by `otuijian`) b on a.tuijian=b.otuijian order by b.count desc");
//        halt($list);
        return $this->fetch('index',['list'=>$list]);   
    }
    public function edit($id){
        $res = db('jianlis')
           ->where('id',$id)
           ->find();
        $res['shixi_start'] = json_decode($res['shixi_start'],true);
        $res['shixi_end'] = json_decode($res['shixi_end'],true);
        $res['shixi_company'] = json_decode($res['shixi_company'],true);
        $res['shixi_zw'] = json_decode($res['shixi_zw'],true);
        $res['shixi_daiyu'] = json_decode($res['shixi_daiyu'],true);
        return $this->fetch('edit',['rows'=>$res]);
    }
    /*
     * 更新'0'或'1'
     */
    public function flag($id){
        if (request()->isPost()) {
            $data = input('post.');
            if (db('jianlis')->where(['id' => $id])->update($data) !== false) {
                $this->success('更新成功');
            } else {
                $this->error('更新失败');
            }
        }
    }
    /*
     * 更新'0','1','2'
     */
    public function status($id){
        if (request()->isPost()) {
            $data = input('post.');
            if (db('jianlis')->where(['id' => $id])->update($data) !== false) {
                $this->success('更新成功');
            } else {
                $this->error('更新失败');
            }
        }
    }
    public function delete($id = 0, $ids = [])
    {
        $id = $ids ? $ids : $id;
        if ($id) {
            if (db('jianlis')->delete($id)) {
                $this->success('删除成功');
            } else {
                $this->error('删除失败');
            }
        } else {
            $this->error('请选择需要删除的项目');
        }
    }
    public function tjlist($tuijian){
        $where = [];
        $title = input('title');
        $title && $where['name'] = ['like',"%$title%"];
        $where['otuijian'] = $tuijian;
        $list = db('users')->where($where)->order('id desc')->paginate(15);
        return $this->fetch('tjlist',['list'=>$list]);
    }
    /**
     * ActionName
     */
    public function downloads(){
        $header[]=array("ID","姓名","手机","邮箱","推荐人数","积分");
        $list= db("jianlis")
            ->alias('a')
            ->join('jianlis b','a.tuijian=b.otuijian')
            ->field(['a.id,a.name,a.tel,a.email,count(distinct b.openid) as count,count(distinct b.openid) as count1'])
            ->group('a.name')
            ->select();

        $data = array_merge_recursive($header,$list);

        // array_unshift($header,$list);
   //      $data = array(
   //  array(NULL, 2010, 2011, 2012),
   //  array('Q1',   12,   15,   21),
   //  array('Q2',   56,   73,   86),
   //  array('Q3',   52,   61,   69),
   //  array('Q4',   30,   32,    0),
   // );
create_xls($data,'statistics.xls');
    }
}